package com.book.service.impl;

import com.book.dao.OrderBookDao;
import com.book.dao.OrderDetailDao;
import com.book.dto.OrderDetailDto;
import com.book.dto.OrderDto;
import com.book.helper.OrderHelper;
import com.book.pojo.OrderBook;
import com.book.pojo.OrderDetail;
import com.book.redis.RedisRandomProvider;
import com.book.service.OrderService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 李锦程
 * @Date 2021/4/26 10:31 下午
 * @Package com.book.service.impl
 * @Version 1.0
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderBookDao bookDao;
    @Autowired
    private RedisRandomProvider randomProvider;

    @Autowired
    private OrderDetailDao orderDetailDao;
    @Override
    public OrderDto getOrderById(Integer id) {
        OrderBook orderBook = bookDao.selectByPrimaryKey(id);
        if (orderBook == null){
            return null;
        }
        return OrderHelper.covertPoDto(orderBook);
    }

    @Override
    public List<OrderDto> getOrderListByUser(String uCode) {
        List<OrderBook > list = bookDao.getListByUCode(uCode);
        if (CollectionUtils.isEmpty(list)){
            return Lists.newArrayList();
        }
        return list.stream().map(OrderHelper::covertPoDto).collect(Collectors.toList());
    }

    @Override
    public Integer updateOrder(OrderBook orderBook) {
        return bookDao.updateByPrimaryKeySelective(orderBook);
    }

    @Override
    public Integer insertSelective(Object o) {
        OrderBook orderBook = OrderHelper.covertDtoPo((OrderDto) o);
        return bookDao.insertSelective(orderBook);
    }

    @Override
    public Integer updateSelective(Object o) {
        return null;
    }

    @Override
    public Object selectById(Object o) {
        return null;
    }

    @Override
    public void saveOrderDetail(List<OrderDetail> details) {
        for (OrderDetail o :
                details) {
            orderDetailDao.insertSelective(o);
        }
    }

    @Override
    public List<OrderDetailDto> getOrderDetailByOrderNo(String orderNo) {
        List<OrderDetail> list = orderDetailDao.getOrderDetailListByOrderNo(orderNo);
        if (CollectionUtils.isEmpty(list)){
            return Lists.newArrayList();
        }
        return list.stream().map(OrderHelper::covertDetailPoDto).collect(Collectors.toList());
    }

    @Override
    public Integer insertOrder(OrderDto orderDto) {
        OrderBook orderBook = OrderHelper.covertDtoPo(orderDto);
        return bookDao.insertSelective(orderBook);
    }

    @Override
    public OrderBook getOrderByNo(String orderNo) {
        return bookDao.getOrderByNo(orderNo);
    }

    @Override
    public List<OrderDetailDto> getOrderDetailByBookCodeList(List<String> bookCodeList) {
        if (CollectionUtils.isEmpty(bookCodeList)){
            return Lists.newArrayList();
        }
        List<OrderDetail> details =orderDetailDao.getOrderDetailByBookCodeList(bookCodeList);
        if (CollectionUtils.isEmpty(details)){
            return Lists.newArrayList();
        }
        return details.stream().map(OrderHelper::covertDetailPoDto).collect(Collectors.toList());
    }

    @Override
    public List<OrderDto> getOrderBySearch(String uCode, String text, Integer status) {
        List<OrderBook> orderBySearch = bookDao.getOrderBySearch(uCode, text, status);
        if (CollectionUtils.isEmpty(orderBySearch)){
            return Lists.newArrayList();
        }
        return orderBySearch.stream().map(OrderHelper::covertPoDto).collect(Collectors.toList());
    }
}
